/*
 * Copyright (c) 2012 SUBSECRETARIA DEFENSORIA DEL ESPACIO PUBLICO - SDEP.
 * All Rights Reserved.
 * This software is the confidential and proprietary information of SDEP
 * Developed By CONSTULT-SOFT S.A, Development Team 2012.
 *
 */
package co.gov.medellin.sdep.sisdep.domain.gestion;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import co.gov.medellin.sdep.sisdep.common.AppConstants;
import co.gov.medellin.sdep.sisdep.domain.BaseController;
import co.gov.medellin.sdep.sisdep.domain.MessageModalWindowController;
import co.gov.medellin.sdep.sisdep.gestion.exception.VehiculoException;
import co.gov.medellin.sdep.sisdep.gestion.exception.VehiculoRuntimeException;
import co.gov.medellin.sdep.sisdep.gestion.service.IVehiculoService;
import co.gov.medellin.sdep.sisdep.model.Vehiculo;

/**
 * @author acespedes
 * 
 */
public class VehiculoController extends BaseController implements
		Serializable {

	/**
	 * generated serial version
	 */
	private static final long serialVersionUID = -3285110767384990403L;
	
	/** bean para el manejo del formulario de busqueda Vehiculo */
	private Vehiculo vehiculoSearch = new Vehiculo();

	/** Objeto contenedor del listado de Vehiculo luego de una consulta. */
	private DataModel vehiculoListModel;

	/** bean para el manejo del formulario de creacion/edicion. Vehiculo */
	private Vehiculo vehiculo = new Vehiculo();

	/** instancia de servicio vehiculoService */
	private IVehiculoService vehiculoService;


	/**
	 * Constructor por defecto, inicializa los elemento basicos del Controlador
	 */
	public VehiculoController() {
		this.vehiculoListModel = new ListDataModel();
		cleanValues();
		cleanSearchValues();
	}

	// --------------------------------------------------------------------
	// --- metodos para atender acciones init()
	// --------------------------------------------------------------------
	
	/**
	 * limpia el contenido de los valores que se ingresan (Create, Update)
	 */
	public final void cleanValues(){
		vehiculo.setId(new Long(0L));
		vehiculo.setIdVehiculoTipo(new Long(0L));
		vehiculo.setPlaca("");
		vehiculo.setMunicipio("");
		vehiculo.setMarca("");
		vehiculo.setModelo("");
		vehiculo.setChasis("");
		vehiculo.setMovil("");
		vehiculo.setDescripcion("");
		vehiculo.setEstado("");
		if(this.vehiculoListModel.getWrappedData() != null) {
			((List)this.vehiculoListModel.getWrappedData()).clear();
		}
	}
	
	/**
	 * limpia el contenido de los valores que se ingresan para buscar
	 */
	public final void cleanSearchValues(){
		vehiculoSearch.setPlaca("");
		if(this.vehiculoListModel.getWrappedData() != null) {
			((List)this.vehiculoListModel.getWrappedData()).clear();
		}
		((MessageModalWindowController) getBean("messageController"))
		.clearMessage();
	}

	/**
	 * 
	 * @return String
	 */
	public String searchVehiculoByKey() {
		String msg = "";
		try {
			List valores = null;
			valores = getVehiculoService().load(1, 10,
					this.vehiculoSearch);
			this.vehiculoListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.vehiculoListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.vehiculoListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.VEHICULO_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String create() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.vehiculo.getId().longValue() == 0L) {
				this.vehiculo.setIdVehiculoTipo(new Long(this.vehiculo.getIdVehiculoTipoStr()));
				getVehiculoService().insert(this.vehiculo);
				msg = AppConstants.SUCCESS_CREATE;
			}
		} catch (VehiculoException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (VehiculoRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		cleanSearchValues();
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage("- Vehiculo " + msg, null, null);
		return AppConstants.VEHICULO_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String update() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.vehiculo.getId().longValue() != 0L) {
				this.vehiculo.setIdVehiculoTipo(new Long(this.vehiculo.getIdVehiculoTipoStr()));
				getVehiculoService().update(this.vehiculo);
				msg = AppConstants.SUCCESS_UPDATE;
			}
		} catch (VehiculoException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (VehiculoRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		cleanSearchValues();
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage("- Vehiculo " + msg, null, null);
		return AppConstants.VEHICULO_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender acciones end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public String createVehiculo() {
		vehiculo = new Vehiculo();
		vehiculo.setId(new Long(0L));
		return AppConstants.VEHICULO_CREATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String updateVehiculo() {
		Map params = getExternalContext().getRequestParameterMap();
		String vehiculoDataIndex = (String) params
				.get("vehiculoDataIndex");
		this.vehiculo = (Vehiculo) ((List) this.vehiculoListModel
				.getWrappedData())
				.get(Integer.parseInt(vehiculoDataIndex));
		this.vehiculo.setIdVehiculoTipoStr(this.vehiculo.getIdVehiculoTipo().toString());
		return AppConstants.VEHICULO_UPDATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String cancelVehiculo() {
		cleanValues();
		cleanSearchValues();
		return AppConstants.VEHICULO_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- getters and setters init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public Vehiculo getVehiculoSearch() {
		return vehiculoSearch;
	}

	/**
	 * 
	 * @param vehiculoSearch
	 */
	public void setvehiculoSearch(Vehiculo vehiculoSearch) {
		this.vehiculoSearch = vehiculoSearch;
	}

	/**
	 * 
	 * @return
	 */
	public DataModel getvehiculoListModel() {
		return vehiculoListModel;
	}

	/**
	 * 
	 * @param vehiculoListModel
	 */
	public void setvehiculoListModel(DataModel vehiculoListModel) {
		this.vehiculoListModel = vehiculoListModel;
	}

	/**
	 * 
	 * @return
	 */
	public Vehiculo getVehiculo() {
		return vehiculo;
	}

	/**
	 * 
	 * @param vehiculo
	 */
	public void setvehiculo(Vehiculo vehiculo) {
		this.vehiculo = vehiculo;
	}

	/**
	 * obtencion del servicio vehiculoService
	 * 
	 * @return
	 */
	public IVehiculoService getVehiculoService() {
		return vehiculoService;
	}

	/**
	 * establecimiento del servicio vehiculoService, la inyeccion de la
	 * dependencia la cubre SpringFramework
	 * 
	 * @param vehiculoService
	 */
	public void setVehiculoService(
			IVehiculoService vehiculoService) {
		this.vehiculoService = vehiculoService;
	}
	
	//TOUCH COMMENT!

	// --------------------------------------------------------------------
	// --- getters and setters end()
	// --------------------------------------------------------------------

}
